﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Linq.Utils;
using System.ComponentModel;
using System.Data.Linq.SqlClient;
using System.Linq.Dynamic;

namespace MyForum.Business.Controllers
{
    public class MessageController : GenericController<Message, MyForumDataContext>
    {
        [DataObjectMethod(DataObjectMethodType.Select)]
        public static IQueryable<Message> Search(string searchPattern)
        {
            string likeSearchPattern = String.Format("%{0}%", searchPattern);

            IQueryable<Message> results = SelectAll().Where(m => SqlMethods.Like(m.Title, likeSearchPattern) ||
                                                                 SqlMethods.Like(m.Description, likeSearchPattern) ||
                                                                 SqlMethods.Like(m.Text, likeSearchPattern));

            return results;
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static IQueryable<Message> Search(string searchPattern, int maximumRows, int startRowIndex)
        {
            #region Validation
            if (maximumRows <= 0)
            {
                throw new ArgumentOutOfRangeException("maximumRows");
            }

            if (startRowIndex < 0)
            {
                throw new ArgumentOutOfRangeException("startRowIndex");
            }
            #endregion

            return Search(searchPattern).Skip(startRowIndex).Take(maximumRows);
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static IQueryable<Message> Search(string searchPattern, string sortExpression)
        {
            if (String.IsNullOrEmpty(sortExpression))
            {
                return Search(searchPattern);
            }

            return Search(searchPattern).OrderBy(sortExpression);
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static IQueryable<Message> Search(string searchPattern, string sortExpression, int maximumRows, int startRowIndex)
        {
            #region Validation
            if (maximumRows <= 0)
            {
                throw new ArgumentOutOfRangeException("maximumRows");
            }

            if (startRowIndex < 0)
            {
                throw new ArgumentOutOfRangeException("startRowIndex");
            }
            #endregion

            if (String.IsNullOrEmpty(sortExpression))
            {
                return Search(searchPattern, maximumRows, startRowIndex);
            }

            return Search(searchPattern, sortExpression).Skip(startRowIndex).Take(maximumRows);
        }

        public static int SearchCount(string searchPattern)
        {
            return Search(searchPattern).Count();
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static IQueryable<Message> GetMessageFromForum(int forumId)
        {
            var results = from m in DataContext.Messages
                          where m.ForumId == forumId &&
                                m.TopicId == null
                          select m;

            return results;
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static IQueryable<Message> GetMessageFromForum(int forumId, int maximumRows, int startRowIndex)
        {
            #region Validation
            if (maximumRows <= 0)
            {
                throw new ArgumentOutOfRangeException("maximumRows");
            }

            if (startRowIndex < 0)
            {
                throw new ArgumentOutOfRangeException("startRowIndex");
            }
            #endregion

            return GetMessageFromForum(forumId).Skip(startRowIndex).Take(maximumRows);
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static IQueryable<Message> GetMessageFromForum(int forumId, string sortExpression)
        {
            if (String.IsNullOrEmpty(sortExpression))
            {
                return GetMessageFromForum(forumId);
            }

            return GetMessageFromForum(forumId).OrderBy(sortExpression);
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static IQueryable<Message> GetMessageFromForum(int forumId, string sortExpression, int maximumRows, int startRowIndex)
        {
            #region Validation
            if (maximumRows <= 0)
            {
                throw new ArgumentOutOfRangeException("maximumRows");
            }

            if (startRowIndex < 0)
            {
                throw new ArgumentOutOfRangeException("startRowIndex");
            }
            #endregion

            if (String.IsNullOrEmpty(sortExpression))
            {
                return GetMessageFromForum(forumId, maximumRows, startRowIndex);
            }

            return GetMessageFromForum(forumId, sortExpression).Skip(startRowIndex).Take(maximumRows);
        }

        public static int GetMessageFromForumCount(int forumId)
        {
            return GetMessageFromForum(forumId).Count();
        }
    }
}
